package com.earnings.okhttputils.utils.ui.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.UserBankData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.dialog.ChooseDialog;

import java.util.Map;


/*
编辑用户信息-银行卡-管理
 */

public class UserEditBankInfoActivity extends GodLeftHandBaseActivity implements View.OnClickListener {
    private TextView bankname,banktype,bankaccount;
    private ImageView logo;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected int onCreateLayoutId() {
        return  R.layout.activity_user_edit_bank_info;
    }

    @Override
    protected void init() {
        setTitle("银行卡服务");
        showRightTextView( "管理");
        findViewById(R.id.top_right_tv).setOnClickListener(this);
        bankname= (TextView) findViewById( R.id.bankname );
        banktype= (TextView) findViewById( R.id.banktype );
        bankaccount= (TextView) findViewById( R.id.bankaccount );

         logo= (ImageView)findViewById( R.id.logo );
      //  rvMsg= (RecyclerView) findViewById(R.id.recyclerview);

        loadBanksData();
    }






    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.top_right_tv) {
            final ChooseDialog exit =new ChooseDialog(getContext(),"是否要解绑该银行卡");
            exit.setSure("确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteBank();
                    exit.dismiss();
                }
            });
            exit.show();
        }
    }

    public void loadBanksData() {
        Map map = new HttpMap(getContext());
        map.put( "id" ,bundleData.getString( "id" ));

        OkHttpUtils.post().url( HttpUrl.USER_BANK_INFO_URL ).params( map ).build().execute( new HttpObjectCallback<UserBankData>(  getContext()) {
            @Override
            public void onSuccess(UserBankData object, String msg) {

                bankname.setText( object.getAccount_tname() );
                banktype.setText( object.getAccount_tsub() );
                bankaccount.setText( "尾号"+object.getAccount().substring( object.getAccount().length()-4,object.getAccount().length() ) );
                Glide.with(getContext()).load(object.getBank_logo() ).error( R.mipmap.head_icon ).into(logo);

            }
            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
            }


        } );

    }
    public void deleteBank() {
        Map map = new HttpMap(getContext());
        map.put( "id" ,bundleData.getString( "id" ));

        OkHttpUtils.post().url( HttpUrl.USER_BANK_DELETE_URL ).params( map ).build().execute( new HttpObjectCallback<String>(  getContext()) {
            @Override
            public void onSuccess(String object, String msg) {

                ToastUtils.showToast( getContext(),msg );
                finish();
            }
            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
            }


        } );

    }
}
